package com.baizhi.service.impl;

import com.baizhi.dao.EmployeeDAO;
import com.baizhi.entity.Employee;
import com.baizhi.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDAO employeeDAO;

    /*查所有*/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Employee> queryAll() {
        return employeeDAO.selectAll();
    }

    /*根据id查询*/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Employee queryOne(Integer eid) {
        return employeeDAO.selectById(eid);
    }

    /*增*/
    @Override
    public void add(Employee employee) {
        employeeDAO.insert(employee);
    }

    /*删*/
    @Override
    public void drop(Integer eid) {
        employeeDAO.delete(eid);
    }

    /*改*/
    @Override
    public void modify(Employee employee) {
        employeeDAO.update(employee);
    }

    /*根据页码查询*/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Employee> queryByPage(int pageNumber) {

        int beginCount = (pageNumber-1)*5;

        return employeeDAO.selectByPage(beginCount,5);
    }

    /*获取数据条数  根据条数获取总页码*/
    @Override
    public int getTotalPage() {

        int totalCount = employeeDAO.selectTotalCount();
        //根据总条数 计算总页数
        int totalPage = 0;
        if(totalCount % 5 == 0){
            totalPage = totalCount / 5 ;
        }else{
            totalPage = totalCount / 5 +1 ;
        }
        
        return totalPage;
    }

    /*批量删除*/
    @Override
    public void plDelete(int[] eids) {
        employeeDAO.deleteMore(eids);
    }
}
